import { WeaTools } from 'ecCom';

/**
 * 获取Cookie值
 *
 * @param {*} name
 */
export const getCookieValue = (name) => {
  let value = '';

  const cookie = window.document.cookie;
  const cookies = cookie.split('; ');
  for (let i = 0, len = cookies.length; i < len; i++) {
    const item = cookies[i].split('=');
    if (item[0] == name) {
      value = item[1];
    }
  }

  if (value == 'true') {
    value = true;
  } else if (value == 'false') {
    value = false;
  }

  return value;
};

/**
 * 获取查询参数
 *
 * @param {*} search
 */
export const getSearchParams = (search = '') => {
  const searchParams = {};

  if (search) {
    const params = search.slice(search.indexOf('?') + 1).split('&');
    for (let i = 0, len = params.length; i < len; i++) {
      const item = params[i].split('=');
      searchParams[item[0]] = item[1];
    }
  }

  return searchParams;
};

/**
 * 获取地址栏查询参数
 */
export const getQueryParams = () => {
  let queryParams = {
    ...getSearchParams(window.location.search),
    ...getSearchParams(window.location.hash),
  };
  // 当启用统一认证时，还需要从统一认证的service参数中提取参数
  if (queryParams.appid && queryParams.service) {
    const service = window.decodeURIComponent(window.decodeURIComponent(queryParams.service));
    const length = service.length;
    const start = service.indexOf('index.html?');
    const end = service.indexOf('#');
    if (start != -1) {
      const search = service.substring(start, end != -1 ? end : length);
      queryParams = {
        ...queryParams,
        ...getSearchParams(search),
      };
    }
  }
  return queryParams;
};

/**
 * 获取本地缓存数据
 *
 * @param {*} key1
 * @param {*} key2
 */
export const getLSValue = (key1, key2) => {
  const obj = WeaTools.ls.getJSONObj(key1);
  return obj && obj[key2];
};

/**
 * 设置本地缓存数据
 *
 * @param {*} key1
 * @param {*} key2
 * @param {*} value
 */
export const setLSValue = (key1, key2, value) => {
  const obj = WeaTools.ls.getJSONObj(key1) || {};
  obj[key2] = value;
  WeaTools.ls.set(key1, obj);
};

/**
 * 初始化本地缓存数据
 *
 * @param {*} loginId
 * @param {*} langId
 */
export const initLSValue = (loginId, langId) => {
  const _loginId = getLSValue('login', 'loginId');
  const _langId = getLSValue('login', 'langId');

  const ls = window.localStorage;
  const keys = [];

  if (!_loginId || !_langId || loginId != _loginId || langId != _langId) {
    keys.push('e9_locale');
    for (let i = 0, len = ls.length; i < len; i++) {
      const key = ls.key(i);
      if (/^theme-/.test(key) || /^portal-/.test(key) || /^edata-/.test(key) || /^tabdata-/.test(key)) {
        keys.push(key);
      }
    }
  }

  for (let i = 0, len = keys.length; i < len; i++) {
    ls.removeItem(keys[i]);
  }

  WeaTools.ls.set('isInitLSValue', true);
};

/**
 * 获取登录页背景图片样式
 *
 * @param {*} loginInfo
 */
export const getLoginBgImageStyle = (loginInfo) => {
  const { fillStyle } = loginInfo;
  const src = getLSValue('login', 'loginBgImage') || loginInfo.imgsrc;
  const width = getLSValue('login', 'loginBgImageWidth') || loginInfo.width;
  const height = getLSValue('login', 'loginBgImageHeight') || loginInfo.height;

  let style = { backgroundImage: `url(${src})` };
  switch (fillStyle) {
    case 'adapt':
    case 'fill': {
      style.backgroundRepeat = 'no-repeat';
      style.backgroundSize = 'cover';
      let _style = {};
      let win_width = jQuery(window).width();
      let win_height = jQuery(window).height();
      win_width = win_width < 800 ? 800 : win_width;
      win_height = win_height < 660 ? 660 : win_height;
      let rate = width / height;
      let _rate = win_width / win_height;
      if (rate > _rate) {
        if (fillStyle == 'adapt') {
          _style = {
            width: win_width,
            height: win_width / rate,
            marginTop: (win_height - win_width / rate) / 2,
          };
        }
        if (fillStyle == 'fill') {
          _style = {
            width: win_height * rate,
            height: win_height,
            marginLeft: -((win_height * rate - win_width) / 2),
          };
        }
      } else if (rate < _rate) {
        if (fillStyle == 'adapt') {
          _style = {
            width: win_height * rate,
            height: win_height,
            marginLeft: (win_width - win_height * rate) / 2,
          };
        }
        if (fillStyle == 'fill') {
          _style = {
            width: win_width,
            height: win_width / rate,
          };
        }
      } else {
        _style = {
          width: win_width,
          height: win_height,
        };
      }
      style = { ...style, ..._style };
      break;
    }
    case 'tile':
      style.backgroundRepeat = 'repeat';
      break;
    case 'center':
      style.backgroundPosition = 'center';
      style.backgroundRepeat = 'no-repeat';
      break;
    default:
      style.backgroundRepeat = 'no-repeat';
      style.backgroundSize = '100% 100%';
      break;
  }
  return style;
};
